<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-title">管理员登录</div>
      <!-- 登录表单 -->
      <el-form
          status-icon
          :model="loginForm"
          :rules="rules"
          ref="ruleForm"
          class="login-form"
      >
        <!-- 用户名输入框 -->
        <el-form-item prop="username">
          <el-input
              v-model="loginForm.username"
              prefix-icon="el-icon-user-solid"
              placeholder="用户名"
              @keyup.enter.native="submit"
          />
        </el-form-item>
        <!-- 密码输入框 -->
        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"
              prefix-icon="iconfont el-icon-mymima"
              show-password
              placeholder="密码"
              @keyup.enter.native="submit"
          />
        </el-form-item>
      </el-form>
      <!-- 登录按钮 -->
      <el-button type="primary" @click="submit">登录</el-button>
    </div>
  </div>
</template>

<script>
import {generaMenu} from "../../assets/js/menu";

export default {
  data: function () {
    return {
      loginForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          {required: true, message: "用户名不能为空", trigger: "blur"}
        ],
        password: [{required: true, message: "密码不能为空", trigger: "blur"}]
      }
    };
  },
  created() {
    if (this.$cookie.get('username') && this.$cookie.get('password')) {
      this.loginForm.username = this.$cookie.get('username')
      this.loginForm.password = this.$cookie.get('password')
      this.login()
    }
  },
  methods: {
    submit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.login()
        } else {
          return false;
        }
      });
    },
    login() {
      const that = this;
      //发送登录请求
      let param = new URLSearchParams();
      param.append("username", that.loginForm.username);
      param.append("password", that.loginForm.password);
      that.axios.post("/api/login", param).then(({data}) => {
        if (data.flag) {
          // 登录后保存用户信息
          that.$store.commit("login", data.data);
          this.$cookie.set('username', that.loginForm.username, 7);
          this.$cookie.set('password', that.loginForm.password, 7);
          // 加载用户菜单
          generaMenu();
          that.$message.success("登录成功");
          that.$router.push({path: "/"});
        } else {
          that.$message.error(data.message);
        }
      });
      // eslint-disable-next-line no-undef
      // var captcha = new TencentCaptcha(
      //   this.config.TENCENT_CAPTCHA,
      //   function(res) {
      //     if (res.ret === 0) {
      //
      //     }
      //   }
      // );
      // // 显示验证码
      // captcha.show();
    }
  }
};
</script>

<style scoped>
.login-container {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  background: url(../../assets/png/commentBack.jpg) center center /
    cover no-repeat;
}

.login-card {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  padding: 170px 60px 180px;
  width: 350px;
}

.login-title {
  color: #ffffff;
  font-weight: bold;
  font-size: 1rem;
}

.login-form {
  margin-top: 1.2rem;
}

.login-card button {
  margin-top: 1rem;
  width: 100%;
}
</style>
